#include "CImageAlgorithm.h"
#include <opencv2/opencv.hpp>
#include <QImage>
#include <QPixmap>
#include <QDebug>

CImageAlgorithm::CImageAlgorithm(QObject *parent)
    : QObject{parent}
{

}

int CImageAlgorithm::findMaxConsecutiveRows(QPixmap &pixmap)
{
    QImage image = pixmap.toImage().convertToFormat(QImage::Format_RGB888);
    cv::Mat cvImage(image.height(), image.width(), CV_8UC3, image.bits(), image.bytesPerLine());


    cv::Mat grayImage;
    cv::cvtColor(cvImage, grayImage, cv::COLOR_BGR2GRAY);

    int maxConsecutiveRows = 0;
//    int maxConsecutiveRowNumber = 0;
    int consecutiveRows = 0;

    for (int row = 1; row < grayImage.rows; row++) {
        cv::Mat rowDiff;
        cv::absdiff(grayImage.row(row), grayImage.row(row - 1), rowDiff);

        if (cv::countNonZero(rowDiff) == 0) {
            consecutiveRows++;
        }
        else {
            if (consecutiveRows > maxConsecutiveRows) {
                maxConsecutiveRows = consecutiveRows;
//                maxConsecutiveRowNumber = row - consecutiveRows;
            }
            consecutiveRows = 0;
        }
    }

    if (consecutiveRows > maxConsecutiveRows) {
        maxConsecutiveRows = consecutiveRows;
//        maxConsecutiveRowNumber = grayImage.rows - consecutiveRows;
    }

    return maxConsecutiveRows;
}

QRect CImageAlgorithm::findDifference(const QStringList &imageList)
{
    if (imageList.count() < 2)
        return QRect();

    cv::Mat referenceImage = cv::imread(imageList.first().toStdString());
    cv::Mat difference = cv::Mat::zeros(referenceImage.size(), CV_8UC3);

    for (int i = 1; i < imageList.size(); ++i) {
        cv::Mat currentImage = cv::imread(imageList[i].toStdString());
        cv::Mat diffMask;

        cv::absdiff(referenceImage, currentImage, diffMask);
        cv::bitwise_or(difference, diffMask, difference);
    }

    cv::Mat grayDifference;
    cv::cvtColor(difference, grayDifference, cv::COLOR_BGR2GRAY);

    cv::Mat binaryDifference;
    cv::threshold(grayDifference, binaryDifference, 1, 255, cv::THRESH_BINARY);


    //find
    // 从上往下找到第一行不全是黑色的像素
    int topBoundary = -1;
    for (int row = 0; row < binaryDifference.rows; ++row) {
        bool hasWhitePixel = false;
        for (int col = 0; col < binaryDifference.cols; ++col) {
            if (binaryDifference.at<uchar>(row, col) == 255) {
                hasWhitePixel = true;
                break;
            }
        }
        if (hasWhitePixel) {
            topBoundary = row;
            break;
        }
    }

    // 从下往上找到第一行不全是黑色的像素
    int bottomBoundary = -1;
    for (int row = binaryDifference.rows - 1; row >= 0; --row) {
        bool hasWhitePixel = false;
        for (int col = 0; col < binaryDifference.cols; ++col) {
            if (binaryDifference.at<uchar>(row, col) == 255) {
                hasWhitePixel = true;
                break;
            }
        }
        if (hasWhitePixel) {
            bottomBoundary = row;
            break;
        }
    }

    qDebug() << "scrollArea x:" << topBoundary << "scrollArea height:" << bottomBoundary - topBoundary;

    // 在原始图像上绘制红色分界线
    cv::line(referenceImage, cv::Point(0, topBoundary), cv::Point(referenceImage.cols - 1, topBoundary), cv::Scalar(0, 0, 255), 2);
    cv::line(referenceImage, cv::Point(0, bottomBoundary), cv::Point(referenceImage.cols - 1, bottomBoundary), cv::Scalar(0, 0, 255), 2);


    cv::imwrite("difference.jpg", difference);
    cv::imwrite("binaryDifference.jpg", binaryDifference);
    cv::imwrite("highlighted_difference.jpg", referenceImage);

    return QRect(0, topBoundary, binaryDifference.cols, bottomBoundary - topBoundary);
}


